xl: don't use libxl allocator for nic_list
author"Gianni Tedesco (3P)" <gianni.tedesco@citrix.com>
Wed, 11 Aug 2010 12:07:21 +0000 (13:07 +0100)
committer"Gianni Tedesco (3P)" <gianni.tedesco@citrix.com>
Wed, 11 Aug 2010 12:07:21 +0000 (13:07 +0100)
This also fixes a bug with an erroneous call to libxl_free().
A destructor for the nic list is also implemented which is called from
xl.

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
tools/libxl/libxl.c
tools/libxl/libxl.h
tools/libxl/libxl_utils.c
tools/libxl/xl_cmdimpl.c

index 5cc003731ca6c27bba777aa7401abc55ac3132fc..6d9869be70a50680d5dc078c436a622fd467490a 100644 (file)
@@ -1887,10 +1887,21 @@ int libxl_device_nic_del(libxl_ctx *ctx,
     return libxl_device_del(ctx, &device, wait);
 }
 
+void libxl_free_nics_list(libxl_nicinfo *nics, unsigned int nb)
+{
+    unsigned int i;
+    for(i = 0; i < nb; i++) {
+        free(nics[i].backend);
+        free(nics[i].frontend);
+        free(nics[i].script);
+    }
+    free(nics);
+}
+
 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
 {
     char *dompath, *nic_path_fe;
-    char **l;
+    char **l, **list;
     char *val, *tok;
     unsigned int nb_nics, i;
     libxl_nicinfo *res, *nics;
@@ -1899,22 +1910,21 @@ libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
     if (!dompath) {
         return NULL;
     }
-    l = libxl_xs_directory(ctx, XBT_NULL,
+    list = l = libxl_xs_directory(ctx, XBT_NULL,
                            libxl_sprintf(ctx, "%s/device/vif", dompath), &nb_nics);
     if (!l) {
         return NULL;
     }
-    res = libxl_calloc(ctx, nb_nics, sizeof (libxl_device_nic));
+    nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
     if (!res) {
         libxl_free(ctx, l);
         return NULL;
     }
-    nics = res;
     for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
         nic_path_fe = libxl_sprintf(ctx, "%s/device/vif/%s", dompath, *l);
 
-        nics->backend = libxl_xs_read(ctx, XBT_NULL,
-                                      libxl_sprintf(ctx, "%s/backend", nic_path_fe));
+        nics->backend = xs_read(ctx->xsh, XBT_NULL,
+                                libxl_sprintf(ctx, "%s/backend", nic_path_fe), NULL);
         val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", nic_path_fe));
         nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
 
@@ -1932,17 +1942,14 @@ libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
         nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
         val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/rx-ring-ref", nic_path_fe));
         nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
-        nics->frontend = libxl_xs_read(ctx, XBT_NULL,
-                                       libxl_sprintf(ctx, "%s/frontend", nics->backend));
+        nics->frontend = xs_read(ctx->xsh, XBT_NULL,
+                                 libxl_sprintf(ctx, "%s/frontend", nics->backend), NULL);
         val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", nics->backend));
         nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
-        nics->script = libxl_xs_read(ctx, XBT_NULL,
-                                     libxl_sprintf(ctx, "%s/script", nics->backend));
-
-        libxl_free(ctx, nic_path_fe);
+        nics->script = xs_read(ctx->xsh, XBT_NULL,
+                               libxl_sprintf(ctx, "%s/script", nics->backend), NULL);
     }
 
-    libxl_free(ctx, l);
     return res;
 }
 
index 1d30abfe9e67bf06f396143b3954821d161bce08..2529f9948633c583f722ce73c0e14d3cabb9160b 100644 (file)
@@ -511,6 +511,7 @@ typedef struct {
 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
 int libxl_device_nic_del(libxl_ctx *ctx, libxl_device_nic *nic, int wait);
 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb);
+void libxl_free_nics_list(libxl_nicinfo *nics, unsigned int nb);
 
 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console);
 
index 4ccca2f2710733177c530af41946739dfc64d82e..1042cc848c8d716be02c52c126badecef420bbc8 100644 (file)
@@ -368,14 +368,14 @@ int libxl_pipe(libxl_ctx *ctx, int pipes[2])
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {
-    libxl_nicinfo *nics;
-    unsigned int nb, i;
+    libxl_nicinfo *nics, *list;
+    unsigned int nb, i, j;
     uint8_t mac_n[6];
     uint8_t *a, *b;
     const char *tok;
     char *endptr;
 
-    nics = libxl_list_nics(ctx, domid, &nb);
+    list = nics = libxl_list_nics(ctx, domid, &nb);
     if (!nics) {
         return ERROR_FAIL;
     }
@@ -387,7 +387,7 @@ int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
         }
     }
     memset(nic, 0, sizeof (libxl_device_nic));
-    for (; nb; --nb, ++nics) {
+    for (j = 0; j < nb; ++j, ++nics) {
         for (i = 0, a = nics->mac, b = mac_n;
              (b < mac_n + 6) && (*a == *b); ++a, ++b)
             ;
@@ -397,12 +397,12 @@ int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
             nic->devid = nics->devid;
             memcpy(nic->mac, nics->mac, sizeof (nic->mac));
             nic->script = nics->script;
-            libxl_free(ctx, nics);
+            libxl_free_nics_list(list, nb);
             return 0;
         }
     }
 
-    libxl_free(ctx, nics);
+    libxl_free_nics_list(list, nb);
     return 0;
 }
 
@@ -422,6 +422,9 @@ int libxl_devid_to_device_nic(libxl_ctx *ctx, uint32_t domid,
     nic_path_be = libxl_xs_read(ctx, XBT_NULL,
                                 libxl_sprintf(ctx, "%s/backend", nic_path_fe));
     val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", nic_path_fe));
+    if ( NULL == val ) {
+        return ERROR_FAIL;
+    }
     nic->backend_domid = strtoul(val, NULL, 10);
     nic->devid = strtoul(devid, NULL, 10);
     libxl_free(ctx, val);
index 206b54332f4e7acddc3f0a0ab55b6d809a934de0..ab794d547bd2495ff6f2185b73d7f83569d48ada 100644 (file)
@@ -4045,8 +4045,8 @@ int main_networkattach(int argc, char **argv)
 int main_networklist(int argc, char **argv)
 {
     int opt;
-    libxl_nicinfo *nics;
-    unsigned int nb;
+    libxl_nicinfo *nics, *list;
+    unsigned int nb, i;
 
     if (argc < 3) {
         help("network-list");
@@ -4071,10 +4071,10 @@ int main_networklist(int argc, char **argv)
             fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
             continue;
         }
-        if (!(nics = libxl_list_nics(&ctx, domid, &nb))) {
+        if (!(list = nics = libxl_list_nics(&ctx, domid, &nb))) {
             continue;
         }
-        for (; nb > 0; --nb, ++nics) {
+        for (i = 0; i < nb; ++i, ++nics) {
             /* Idx BE */
             printf("%-3d %-2d ", nics->devid, nics->backend_id);
             /* MAC */
@@ -4086,6 +4086,7 @@ int main_networklist(int argc, char **argv)
                    nics->devid, nics->state, nics->evtch,
                    nics->rref_tx, nics->rref_rx, nics->backend);
         }
+        libxl_free_nics_list(list, nb);
     }
     return 0;
 }